//
//  MultiSelectedView.m
//  AlarmClock
//
//  Created by apple on 16/10/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "MultiSelectedView.h"
#import "MultiSelectTableViewCell.h"
#import "SDAutoLayout.h"
@interface MultiSelectedView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UIButton *cancelButton;
@property (nonatomic,strong) UIButton *sureButton;
@property (nonatomic,strong) NSArray *dataSource;
@property (nonatomic,strong) NSMutableArray *selectedData;
@end

@implementation MultiSelectedView
- (void)show:(CGRect)rect title:(NSString *)title dataSource:(NSArray *)dataSource {
    self.selectedData = @[].mutableCopy;
    [self addSubview:self.backView];
    self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.51];
    self.backView.frame = rect;
    self.backView.backgroundColor = [UIColor whiteColor];
    self.dataSource = dataSource;
    self.titleLabel.text = title;
    
    self.alpha = 0;
    self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
    
    [self.backView addSubview:self.titleLabel];
    [self.backView addSubview:self.tableView];
    [self.backView addSubview:self.cancelButton];
    [self.backView addSubview:self.sureButton];
    
    self.titleLabel.sd_layout
    .topSpaceToView(self.backView,20)
    .leftSpaceToView(self.backView,0)
    .widthIs(rect.size.width)
    .heightIs(30);
    
    self.tableView.sd_layout
    .leftSpaceToView(self.backView,0)
    .rightSpaceToView(self.backView,0)
    .topSpaceToView(self.titleLabel,0)
    .heightIs(rect.size.height - 110);
    
    self.cancelButton.sd_layout
    .leftSpaceToView(self.backView,20)
    .widthIs((rect.size.width - 50)/2)
    .bottomSpaceToView(self.backView,10)
    .heightIs(rect.size.width / 6);
    
    self.sureButton.sd_layout
    .rightSpaceToView(self.backView,20)
    .widthIs((rect.size.width - 50)/2)
    .bottomSpaceToView(self.backView,10)
    .heightIs(rect.size.width / 6);
    
    [UIView animateWithDuration:0.4 animations:^{
        self.backView.transform = CGAffineTransformMakeScale(1, 1);
        self.alpha = 1;
    }];
    
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [[event touchesForView:self] anyObject];
    if (touch) {
        [UIView animateWithDuration:0.3 animations:^{
            self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
    
}

#pragma mark - action
- (void)cancelButtonPressed:(UIButton *)sender {
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)sureButtonPressed:(UIButton *)sender {
    
    NSArray *array = [self.tableView indexPathsForSelectedRows];
    for (NSIndexPath *indexPath in array) {
        [self.selectedData addObject:self.dataSource[indexPath.row]];
    }
//    NSString *string = @"";
//    if (self.selectedData.count > 0) {
//        NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:@"alarmClock"];
//        for (NSDictionary *dic in array) {
//            if ([dic[@"repeat"] isEqualToString:self.defaultSelectString]) {
//                string = dic[@"repeat"];
//            }
//        }
//    }
    if (_multiSelectedHandle) {
        _multiSelectedHandle(self.selectedData);
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];

}

#pragma mark - delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"multi";
     MultiSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[MultiSelectTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.titleLabel.text = self.dataSource[indexPath.row];
    NSArray *array = [self.defaultSelectString componentsSeparatedByString:@"|"];
    for (NSString *string in array) {
        if ([self.dataSource[indexPath.row] isEqualToString:string]) {
            [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        } else {
            cell.selected = NO;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    
}


#pragma mark - getter
- (UIView *)backView {
    if (_backView) {
        return _backView;
    }
    _backView = [[UIView alloc]init];
    _backView.layer.masksToBounds = YES;
    _backView.layer.cornerRadius = 10;
    return _backView;
}
- (UILabel *)titleLabel {
    if (_titleLabel) {
        return _titleLabel;
    }
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    return _titleLabel;
}

- (UITableView *)tableView {
    if (_tableView) {
        return _tableView;
    }
    _tableView = [[UITableView alloc]init];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.allowsMultipleSelection = YES;
    _tableView.rowHeight = 70;
    return _tableView;
}

- (UIButton *)cancelButton {
    if (_cancelButton) {
        return _cancelButton;
    }
    _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelButton = [[UIButton alloc]init];
    _cancelButton.backgroundColor = [UIColor redColor];
    _cancelButton.layer.masksToBounds = YES;
    _cancelButton.layer.cornerRadius = 10;
    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    return _cancelButton;
}

- (UIButton *)sureButton {
    if (_sureButton) {
        return _sureButton;
    }
    _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureButton = [[UIButton alloc]init];
    _sureButton.layer.masksToBounds = YES;
    _sureButton.layer.cornerRadius = 10;
    _sureButton.backgroundColor = [UIColor redColor];
    [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [_sureButton addTarget:self action:@selector(sureButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    return _sureButton;
}
@end
